/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import bean.Jardin;
import bean.Legume;
import bean.Panier;
import java.io.IOException;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Hippolyte
 */
@WebServlet(name = "ServletControleur", urlPatterns = {"/ServletControleur"})
public class ServletControleur extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        HttpSession session = request.getSession(true);

        System.out.println("page ==> " + request.getParameter("page"));

        Jardin monJardin = (Jardin) session.getAttribute("monJardin");

        Map<String, String[]> parameterMap = request.getParameterMap();


        if (request.getParameter("page").equals("commande")) {


            Panier monPanier = (Panier) session.getAttribute("monPanier");

            Legume l;
            String qteS;
            int qte;

            if (parameterMap.containsKey("bt_consulter")) {
                session.setAttribute("monPanier", monPanier);
                RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/vueJSP.jsp");
                disp.forward(request, response);
            }


            for (String key : parameterMap.keySet()) {

                if (key.startsWith("qt_")) {

                    qteS = (String) parameterMap.get(key)[0];

                    l = monJardin.getLegume(key.replace("qt_", ""));

                    qte = monJardin.checkQuantite(l, qteS);

                    System.out.println("qte => " + qte);

                    monJardin.vendre(l, qte);

                    if (qte > 0) {

                        monPanier.ajouter(l, qte);
                    }
                }
            }

            monPanier.calculPanier();

            monJardin.sauvJardin();

            session.setAttribute("monPanier", monPanier);
            RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/vueJSP.jsp");
            disp.forward(request, response);

        } else if (request.getParameter("page").equals("index")) {

            session.setAttribute("monJardin", monJardin);

            if (request.getParameterMap().containsKey("bt_commencer")) {
                String nomPanier = request.getParameter("userName");

                Panier monPanier = new Panier(nomPanier);
                
                monPanier.initPanierSQL();


                session.setAttribute("monPanier", monPanier);

                RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/commande.jsp");
                disp.forward(request, response);
            }

            if (request.getParameterMap().containsKey("bt_admin")) {
                RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/admin.jsp");
                disp.forward(request, response);
            }



        } else if (request.getParameter("page").equals("admin")) {

            if (request.getParameterMap().containsKey("bt_ajouter")) {

                Legume l = new Legume(request.getParameter("newLegume"),
                        Integer.parseInt(request.getParameter("prix")));



                monJardin.ajoutLegume(l, Integer.parseInt(request.getParameter("qte")));

                session.setAttribute("monJardin", monJardin);
                RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/admin.jsp");
                disp.forward(request, response);

            }

            if (request.getParameterMap().containsKey("bt_valider")) {

                session.setAttribute("monJardin", monJardin);
                RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/index.jsp");
                disp.forward(request, response);
            }

        } else if (request.getParameter("page").equals("vue")) {
            
            if (request.getParameterMap().containsKey("bt_accueil")) {
                session.setAttribute("monJardin", monJardin);
                RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/index.jsp");
                disp.forward(request, response);
            }

            for (String key : parameterMap.keySet()) {

                if (key.startsWith("bt_ret")) {


                    Panier monPanier = (Panier) session.getAttribute("monPanier");


                    if (key.startsWith("bt_ret1_")) {

                        System.out.println("retire 1 " + key.replace("bt_ret1_", ""));

                        Legume l = monPanier.getLegume(key.replace("bt_ret1_", ""));

                        monPanier.retirer(l, 1);

                        monJardin.ajoutLegume(l, 1);

                        session.setAttribute("monJardin", monJardin);
                        RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/vueJSP.jsp");
                        disp.forward(request, response);
                    }
                    if (key.startsWith("bt_retTout_")) {

                        System.out.println("retire tous les "
                                + key.replace("bt_retTout_", ""));

                        Legume l = monPanier.getLegume(key.replace("bt_retTout_", ""));
                        
                        monJardin.ajoutLegume(l, monPanier.get(l));
                        
                        monPanier.retirer(l, monPanier.get(l));

                    }
                    
                    
                    
                    session.setAttribute("monJardin", monJardin);
                    RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/vueJSP.jsp");
                    disp.forward(request, response);
                }
            }
            


        }
        monJardin.sauvJardin();




    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
